package com.entrogy.controller;

import com.alibaba.fastjson.JSONObject;
import com.entrogy.bean.ClassEntity;
import com.entrogy.config.Result;
import com.entrogy.service.LeaveService;
import com.entrogy.service.MessageService;
import com.entrogy.service.UserService;
import com.entrogy.service.VisitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(description = "教师审核请假申请")
@RestController
@RequestMapping(value = "/checkleave")
public class CheckLeaveController {

	private static final Logger logger = LoggerFactory.getLogger(CheckLeaveController.class);

	@Autowired
	private LeaveService leaveService;
	@Autowired
	private VisitService visitService;//
	@Autowired
	private MessageService messageService;

	@Autowired
	private UserService userService;


	@ApiOperation(value = "请假页面初始化")
	@ApiImplicitParam(name = "uid",value = "老师ID",dataType = "query",paramType = "String",required = true)
	@RequestMapping(value = "/leaveinit",method = RequestMethod.GET)
	public JSONObject leaveInit(String uid) {
		JSONObject returnObj = new JSONObject();
		try {
			returnObj = leaveService.leaveInit(uid);
		} catch (Exception e) {
			returnObj.put("errcode", 1);
			returnObj.put("errmsg", "请假页面初始化失败");
			logger.error("leaveInit:" + uid);
			e.printStackTrace();
			return returnObj;
		}
		returnObj.put("errcode", 0);
		returnObj.put("errmsg", "ok");
		return returnObj;
	}

	@ApiOperation(value = "请假页面翻页")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "uid",value = "老师ID",dataType = "query",paramType = "String",required = true),
			@ApiImplicitParam(name = "currentPage",value = "当前页数",dataType = "query",paramType = "int",required = true),
			@ApiImplicitParam(name = "pageSize",value = "单页长度",dataType = "query",paramType = "int",required = true)

	})
	@RequestMapping(value = "/leavenewpage",method = RequestMethod.GET)
	public JSONObject leaveNewPage(String uid,int currentPage,int pageSize){
		JSONObject returnObj = new JSONObject();
		try {
			returnObj = leaveService.leaveNewPage(uid,currentPage,pageSize, null);
		} catch (Exception e) {
			returnObj.put("errcode", 1);
			returnObj.put("errmsg", "获取请假页面翻页数据失败");
			logger.error("leaveNewPage:" + uid + " " + currentPage);
			e.printStackTrace();
			return returnObj;
		}
		returnObj.put("errcode", 0);
		returnObj.put("errmsg", "ok");
		return returnObj;
	}

	@ApiOperation(value = "请假审核")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "uid",value = "老师ID",paramType = "query",dataType = "String",required = true),
			@ApiImplicitParam(name = "listId",value = "行id",paramType = "query",dataType = "String",required = true),
			@ApiImplicitParam(name = "deal",value = "审核状态",dataType = "int",paramType = "query",required = true)
	})
	@RequestMapping(value = "/check",method = RequestMethod.GET)
	public Result check(String uid, String listId, int deal) {
		logger.info("check:" + uid + " " + listId + " " + deal);
		JSONObject result=leaveService.check(uid, listId, deal);
		return  new Result().getResult(Integer.parseInt(result.get("errcode").toString()),result.get("errmsg").toString(),1,result);

	}

	// 请假页面筛选
	@ApiOperation(value = "请假查看")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "uid", dataType = "String", paramType = "query", value = "ID", required = true),
			@ApiImplicitParam(name = "course_id", value = "班级id", paramType = "query",dataType = "String", required = true),
			@ApiImplicitParam(name = "selectDate", value = "要查询的日期", paramType = "query",dataType = "String"),
			@ApiImplicitParam(name = "isAgree",value = "是否审批通过", paramType = "query",dataType = "Integer", required = true),
	})
	@RequestMapping(value = "/qingjiaSelect",method = RequestMethod.GET)
	public Result qingjiaSelect(String course_id, String uid, String selectDate, Integer isAgree){
       String schoolCode = String.valueOf(userService.getSchoolCodeByUserId(uid));
		List<JSONObject> list = leaveService.getLeaveRecord(course_id, schoolCode, selectDate, isAgree);
		return new Result().getResult(1,"请假页面查询成功",1, list);

	}

	// 请假页面筛选
	@ApiOperation(value = "返回该校的所有年级 ")
	@ApiImplicitParam(name = "uid",value = "ID",required = true,paramType = "query",dataType = "String")
	@PostMapping(value = "/selectClass" )
	public Result selectClass(String uid) {
		try {
			String schoolCode = String.valueOf(userService.getSchoolCodeByUserId(uid));
			Map<String, Object> map = new HashMap<>();
			map.put("schoolCode", schoolCode);
			map.put("NotparentId", 1);

			/*js.put("id","id");*/
			/*js.put("col1","宿舍");*/
			JSONObject js=new JSONObject();
			List<ClassEntity> list = messageService.getClassList1(map);
			if (list!=null){
				js.put("list", list);
			}
			String strings="班级名称,学生姓名,类型,请假时间";
			js.put("js1",strings.split(","));
			return new Result().getResult(1, "班级查询成功", 1, js);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result().getResult(0, "班级查询失败", 0, "");
		}
	}


}
